<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;

class Article extends cmsBase {

    public function index(){
        if(empty($_GET['name'])){
            $data = Db::name('cms_article')->where('status','not in',2)->order('id','desc')->paginate(25);
        }else{
            $data = Db::name('cms_article')
                ->where('title','like',"%{$_GET['name']}%")
                ->where('status','not in',2)
                ->order('id','desc')
                ->paginate(20,false,['query'=>$_GET]);
        }

        $type = Db::name('cms_type')->where('status','not in',2)->select();
        foreach($type as $k => $v){
            $ty[$v['id']] = $v['typename'];
        }
        $page = $data->render();
        return view('article/index',['data'=>$data,'page'=>$page,'ty'=>$ty]);
    }

    public function articleView(){
        $type = Db::name('cms_type')->where('status','not in',2)->select();
        return view('article/addarticle',['type'=>$type]);
    }

    public function addArticle(){
        $file = $this->request->file('img');
        $data = [];
        $data = [
            'title' => $_POST['title'],
            'classification' => $_POST['classification'],
            'content' => $_POST['editorValue'],
            'createtime'=>date('Y-m-d H:i:s'),
            'publisher' => 12,
            'status' => $_POST['status'],
            'synopsis' => $_POST['synopsis'],
        ];
        if($file){
            if($_POST['synopsis']==null) return "<script>alert('有首图文章必须添加简介');history.go(-1);</script>";
            $info = $file->validate(['ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'upload/Article');
            if($info){
                //成功上传后 获取上传信息
                //输出 jpg
                $info->getExtension();
                //输出 42a79759f284b767dfcb2a0197904287.jpg
                $img=$info->getFilename();
                $data['photo'] = date('Ymd').'/'.$img;
            }
        }else{
            $data['photo']  = '';
        }
        // dump($data);die;
        $a = Db::name('cms_article')->insert($data);
        if($a){
            return "<script>alert('新增成功');parent.location.reload();</script>";
        }else{
            return "<script>alert('添加失败');history.go(-1);</script>";
        }
    }

    public function editArticle(){
        if(empty($_POST)){
            $edit = Db::name('cms_article')->where('id',$_GET['id'])->find();
            $type = Db::name('cms_type')->where('status','not in',2)->select();
            return view('article/edit',['edit'=>$edit,'type'=>$type]);
        }else{
//            dump($_POST);die;
            $file = $this->request->file('imgs');
            $image = Db::name('cms_article')->where(['id'=>$_POST['id']])->find()['photo'];
            // echo $image;die;
            $data = [
                'title' => $_POST['title'],
                'classification' => $_POST['classification'],
                'content' => $_POST['editorValue'],
                'createtime'=>date('Y-m-d H:i:s'),
//                'createtime' => $_POST['date'],
                'synopsis' => $_POST['synopsis'],
                'status' => $_POST['status']
            ];
            if($file){
                $info = $file->validate(['ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'upload/Article');
                if($info){
                    //成功上传后 获取上传信息
                    //输出 jpg
                    $info->getExtension();
                    //输出 42a79759f284b767dfcb2a0197904287.jpg
                    $img=$info->getFilename();
                    $data['photo'] = date('Ymd').'/'.$img;
                    if($image!=null){
                        unlink($_SERVER["DOCUMENT_ROOT"].'/upload/Article/'.$image);
                    }

                }
            }

            $up = Db::name('cms_article')->where('id',$_POST['id'])->update($data);
            if($up){
                return "<script>alert('新增成功');parent.location.reload();</script>";
            }else{
                return "<script>alert('添加失败');history.go(-1);</script>";
            }
        }

    }
    //del
    public function delArticle(){
        $del = Db::name('cms_article')->where('id',$_POST['id'])->update(['status'=>2]);
        if($del){
            return 'success';
        }else{
            return 'error';
        }
    }

    public function shopArticle(){
        $shop = Db::name('cms_article')->where('id',$_GET['id'])->update(['status'=>0]);
        if($shop){
            return "<script>window.location.href = document.referrer;</script>";
        }else{
            return "<script>window.location.href = document.referrer;</script>";
        }
    }

    public function  openArticle(){
        $shop = Db::name('cms_article')->where('id',$_GET['id'])->update(['status'=>1]);
        if($shop){
            return "<script>window.location.href = document.referrer;</script>";
        }else{
            return "<script>window.location.href = document.referrer;</script>";
        }
    }
}